import { Module } from 'vuex'
import { IIdPayload, IManageState, IPagePayload } from './types'
import { IRootState } from '../../type'

import {
  getPageList,
  changeStatus,
  deletePageData,
  newPageData,
  resetPw,
  editPageData
} from '@/service/main/manage/manage'
import store from '@/store'

const manageModule: Module<IManageState, IRootState> = {
  namespaced: true,
  state() {
    return {
      userWorkplaceTotalCount: 0,
      userWorkplaceList: [],
      usersTotalCount: 0,
      usersList: [],
      deviceList: [],
      deviceTotalCount: 0
    }
  },
  mutations: {
    changeUserWorkplaceTotalCount(state, totalCount: number) {
      state.userWorkplaceTotalCount = totalCount
    },
    changeUserWorkplaceList(state, userWorkplaceList: any) {
      state.userWorkplaceList = userWorkplaceList
    },
    changeUsersTotalCount(state, totalCount: number) {
      state.usersTotalCount = totalCount
    },
    changeUsersList(state, userList: any) {
      state.usersList = userList
    },
    changeDeviceList(state, deviceList: any) {
      state.deviceList = deviceList
    },
    changeDeviceTotalCount(state, totalCount: number) {
      state.deviceTotalCount = totalCount
    }
  },
  getters: {
    pageListData(state) {
      return (pageName: string) => {
        const listData: any[] = (state as any)[`${pageName}List`] ?? []
        return listData
      }
    },
    pageListDataCount(state) {
      return (pageName: string) => {
        const listCount = (state as any)[`${pageName}TotalCount`] ?? 0
        return listCount
      }
    }
  },
  actions: {
    async getPageListDataAction({ commit }, payload: IPagePayload) {
      const pageName = payload.pageName
      const pageUrl = `/backend/${pageName}List`
      if (pageUrl.length === 0) return
      const result: any = await getPageList(pageUrl, payload.queryInfo)
      const { totalCount, list } = result.data.data

      switch (payload.pageName) {
        case 'userWorkplace':
          commit('changeUserWorkplaceTotalCount', totalCount)
          commit('changeUserWorkplaceList', list)
          break
        case 'users':
          commit('changeUsersTotalCount', totalCount)
          commit('changeUsersList', list)
          break
        case 'device':
          commit('changeDeviceList', list)
          commit('changeDeviceTotalCount', totalCount)
          break
      }
    },

    async changeStatusAction({ dispatch }, payload: IPagePayload) {
      const userWorkplaceId = payload.id
      const email = store.state.email
      console.log('userWorkplaceId', email)
      const pageUrl = `/backend/changeUserOrgStatus/${userWorkplaceId}`
      await changeStatus(pageUrl)
      dispatch('getPageListDataAction', {
        pageName: payload.pageName,
        queryInfo: { offset: 0, size: 10, adminEmail: email }
      })
    },

    async changeLimitAction({ dispatch }, payload: IPagePayload) {
      const userId = payload.id
      const pageUrl = `/backend/changeUserLimit/${userId}`
      await changeStatus(pageUrl)
      dispatch('getPageListDataAction', {
        pageName: payload.pageName,
        queryInfo: payload.queryInfo
      })
    },

    async resetPwAction(context, payload: IIdPayload) {
      const userId = payload.id
      const pageUrl = `/backend/resetPw/${userId}`
      await resetPw(pageUrl)
    },

    async deletePageDataAction({ dispatch }, payload: IPagePayload) {
      const pageName = payload.pageName
      const deleteId = payload.id
      if (!deleteId) return
      const pageUrl = `/backend/${pageName}/${deleteId}`
      await deletePageData(pageUrl)
      dispatch('getPageListDataAction', {
        pageName: payload.pageName,
        queryInfo: { offset: 0, size: 10 }
      })
    },

    async newPageDataAction({ dispatch }, payload: IPagePayload) {
      const pageUrl = `/backend/add/${payload.pageName}`
      const pageData = payload.queryInfo
      await newPageData(pageUrl, pageData)

      dispatch('getPageListDataAction', {
        pageName: payload.pageName,
        queryInfo: { offset: 0, size: 10 }
      })
    },

    async editPageDataAction({ dispatch }, payload: IPagePayload) {
      if (!payload.id) return
      const pageUrl = `/backend/${payload.pageName}/${payload.id}`
      const pageData = payload.queryInfo
      await editPageData(pageUrl, pageData)

      dispatch('getPageListDataAction', {
        pageName: payload.pageName,
        queryInfo: { offset: 0, size: 10 }
      })
    }
  }
}

export default manageModule
